import {Injectable} from '@angular/core';
import {
  HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpParams
} from '@angular/common/http';

import {Observable} from 'rxjs';

/** Pass untouched request through to the next request handler. */
@Injectable()
export class NullValueInterceptor implements HttpInterceptor {

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    let cleanedParams = new HttpParams();
    req.params.keys().forEach(x => {
      if (req.params.get(x)) {
        cleanedParams = cleanedParams.append(x, req.params.get(x));
      }
    });
    req = req.clone({headers:req.headers, params: cleanedParams});
    return next.handle(req);
  }
}
